package com.zhiche.lisa.bms.service.bill.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.zhiche.lisa.bms.dao.mapper.bill.BillIoDetailMapper;
import com.zhiche.lisa.bms.dao.model.bill.BillIoDetail;
import com.zhiche.lisa.bms.service.bill.BillIoDetailService;
import com.zhiche.lisa.core.supports.BaseException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 客户或承运商账单明细 服务实现类
 * </p>
 *
 * @author WangShuiJin
 * @since 2018-09-06
 */
@Service
public class BillIoDetailServiceImpl extends ServiceImpl<BillIoDetailMapper, BillIoDetail> implements BillIoDetailService {

    /**
     * 通过账单编号，得到客户或承运商，账明细列表
     *
     * @param page   分页/查询条件
     * @param headId
     * @return
     */
    @Override
    public Page<BillIoDetail> queryBillIoDetailPage(Page<BillIoDetail> page, Long headId) {
        if (ObjectUtils.isEmpty(headId)) {
            throw new BaseException("账单ID，不能为空！");
        }

        EntityWrapper<BillIoDetail> ew = this.queryCheck(headId);
        ew.orderBy("dispatch_code asc, ship_code asc");
        //清空page附加条件值
        page.setCondition(Maps.newHashMap());
        page.setRecords(baseMapper.selectPage(page, ew));
        return page;
    }

    /**
     * 通过账单编号，得到客户或承运商，账明细列表
     *
     * @param headId
     * @return
     */
    @Override
    public List<BillIoDetail> queryBillIoDetailList(Long headId) {
        if (ObjectUtils.isEmpty(headId)) {
            throw new BaseException("账单ID，不能为空！");
        }

        EntityWrapper<BillIoDetail> ew = this.queryCheck(headId);
        ew.orderBy("dispatch_code asc, ship_code asc");
        //清空page附加条件值
        return baseMapper.selectList(ew);
    }

    /**
     * 新增客户或承运商,账单明细
     *
     * @param billIoDetailList
     * @param headId
     */
    @Override
    public void insertBillIoDetail(List<BillIoDetail> billIoDetailList, Long headId) {
        billIoDetailList.forEach(billIoDetailData -> {
            billIoDetailData.setHeadId(headId);
            boolean insert = this.insert(billIoDetailData);
            if (!insert) {
                throw new BaseException("新增客户或承运商账单明细，失败！");
            }
        });
    }

    /**
     * 根据主表id获取明细数据
     *
     * @param headId
     * @return
     */
    @Override
    public List<BillIoDetail> getBillIoDetailByHeadId(Long headId) {
        if (headId != null) {
            EntityWrapper<BillIoDetail> ew = new EntityWrapper<>();
            ew.eq("head_id", headId);
            return baseMapper.selectList(ew);
        }
        return null;
    }

    /**
     * 查询条件
     *
     * @param headId
     * @return
     */
    private EntityWrapper<BillIoDetail> queryCheck(Long headId) {
        EntityWrapper<BillIoDetail> ew = new EntityWrapper<>();
        // ew.orderBy("gmt_create desc, id desc");
        //账单ID
        ew.eq("head_id", headId);
        return ew;
    }
}
